import { notification, message } from 'antd';
import cloneDeep from 'lodash/cloneDeep';
import _map from 'lodash/map';
import _uniq from 'lodash/uniq';
import _pull from 'lodash/pull';
import { GetDateThree, FormatnumberZero } from '../../../../function';
import {
  getUBSPosition,
  getFssFundOptionsList,
  getGroupFu,
  getWaitBuy,
  addWaitBuy,
  deleteWaitBuy,
  deleteAllWaitBuy,
  getFuGroupHeadETF,
  addFuGroupHeadETF,
  deleteFuGroupHeadETF,
  deleteFuGroupHeadETFAll,
  getUBSNoTwoPosition,
  getFundTwoStockPosition,
  getUBSNewTwoPosition,
  updateFundTwoInfo,
  getLastTrade,
  updateLastTrade,
  insert,
  insertValueChart,
  updateStockUpdateInfo,
  getTradeList,
  getTodayTradeList,
  getLastDayOptionList,
  makeOptionTodayInfo,
  updateOptionFlag,
  syncLastOptionTradeInfo,
} from '../../../../services/allposition_service';

export default {
  namespace: 'fu',
  state: {
    stocks: { code: 0, message: '', list: [] },
    stocksFilters: {},
    newTwoStocks: { code: 0, message: '', list: [] },
    newTwoStocksFilters: {},
    options: { code: 0, message: '', list: [] },
    optionsFilters: {},
    charts: [],
    waitbuy: { code: 0, message: '', inx: 0, list: [] },
    waitsale: { code: 0, message: '', inx: 0, list: [] },
    fuGroupHeadETF: { code: 0, message: '', inx: 0, list: [] },
    newTwoPosition: {
      infos: { code: 0, message: '', list: [] },
      etf: { code: 0, message: '', list: [] },
      bellWether: { code: 0, message: '', list: [] },
    },
    newTwoStocksPositionFilters: {},
    newTwoPositionModal: { code: 0, message: '', list: [] },
  },

  effects: {
    // stock
    *GetUBSPosition({ payload, callback }, { call, put }) {
      const response = yield call(getUBSPosition, payload);
      if (typeof response === 'undefined') {
        return;
      }
      if (typeof response !== 'undefined' && response.code === 1) {
        // 添加敞口
        let allstockopen = 0;
        for (let i = 0; i < response.list.positionList.length; i++) {
          allstockopen += response.list.positionList[i].costmoney || 0;
        }
        for (let i = 0; i < response.list.positionList.length; i++) {
          response.list.positionList[i].stockopen =
            (response.list.positionList[i].costmoney || 0) / allstockopen;
        }
        if (callback) callback(response);
      } else {
        notification.error({
          message: typeof response.message !== 'undefined' ? response.message : '请求错误',
        });
      }
    },

    *GetUBSNoTwoPosition({ payload, callback }, { call, put }) {
      const response = yield call(getUBSNoTwoPosition, payload.param);
      if (typeof response === 'undefined') {
        return;
      }

      if (typeof response !== 'undefined' && response.code === 1) {
        // 添加敞口
        let allstockopen = 0;
        for (let i = 0; i < response.list.length; i++) {
          allstockopen += response.list[i].costmoney || 0;
        }
        for (let i = 0; i < response.list.length; i++) {
          response.list[i].stockopen = (response.list[i].costmoney || 0) / allstockopen;
        }
        // 筛选合计
        let thisincome = 0;
        let allincome = 0;
        let costmoney = 0;
        if (payload.currentDataSource.length === 0) {
          for (let i = 0; i < response.list.length; i++) {
            thisincome += parseFloat(response.list[i].thisincome || 0);
            allincome += parseFloat(response.list[i].allincome || 0);
            costmoney += parseFloat(response.list[i].costmoney || 0);
          }
        } else {
          const stock = _pull(_uniq(_map(payload.currentDataSource, 'stock')), null, undefined);
          for (let i = 0; i < response.list.length; i++) {
            if (stock.indexOf(response.list[i].stock) !== -1) {
              thisincome += parseFloat(response.list[i].thisincome || 0);
              allincome += parseFloat(response.list[i].allincome || 0);
              costmoney += parseFloat(response.list[i].costmoney || 0);
            }
          }
        }
        response.list.unshift({
          stock: '股票合计',
          thisincome,
          allincome,
          costmoney,
        });
        yield put({
          type: 'saveNewTwoStocks',
          payload: {
            list: { code: 1, message: response.message, list: response.list },
            filter: {},
          },
        });
        if (callback) callback(response);
      } else {
        notification.error({
          message: typeof response.message !== 'undefined' ? response.message : '请求错误',
        });
      }
    },

    *GetFundTwoStockPosition({ payload, callback }, { call, put }) {
      const response = yield call(getFundTwoStockPosition, payload);
      if (typeof response === 'undefined') {
        return;
      }

      if (typeof response !== 'undefined' && response.code === 1) {
        // 添加敞口
        let allstockopen = 0;
        for (let i = 0; i < response.list.length; i++) {
          allstockopen += response.list[i].costmoney || 0;
        }
        for (let i = 0; i < response.list.length; i++) {
          response.list[i].stockopen = (response.list[i].costmoney || 0) / allstockopen;
        }
        // 筛选合计
        let thisincome = 0;
        let allincome = 0;
        let costmoney = 0;
        if (payload.currentDataSource.length === 0) {
          for (let i = 0; i < response.list.length; i++) {
            thisincome += parseFloat(response.list[i].thisincome || 0);
            allincome += parseFloat(response.list[i].allincome || 0);
            costmoney += parseFloat(response.list[i].costmoney || 0);
          }
        } else {
          const stock = _pull(_uniq(_map(payload.currentDataSource, 'stock')), null, undefined);
          for (let i = 0; i < response.list.length; i++) {
            if (stock.indexOf(response.list[i].stock) !== -1) {
              thisincome += parseFloat(response.list[i].thisincome || 0);
              allincome += parseFloat(response.list[i].allincome || 0);
              costmoney += parseFloat(response.list[i].costmoney || 0);
            }
          }
        }
        response.list.unshift({
          stock: '股票合计',
          thisincome,
          allincome,
          costmoney,
        });
        yield put({
          type: 'saveNewTwoStocks',
          payload: {
            list: { code: 1, message: response.message, list: response.list },
            filter: {},
          },
        });
        if (callback) callback(response);
      } else {
        notification.error({
          message: typeof response.message !== 'undefined' ? response.message : '请求错误',
        });
      }
    },

    *GetUBSNewTwoPosition({ payload, callback }, { call, put }) {
      const response = yield call(getUBSNewTwoPosition, payload);
      if (typeof response === 'undefined') {
        return;
      }
      if (
        typeof payload.currentDataSourceETF !== 'undefined' &&
        typeof payload.currentDataSourceBellWether !== 'undefined'
      ) {
        yield put({
          type: 'newTwoPositionInfos',
          payload: {
            list: {
              code: 1,
              message: response.message,
              list: response.data.infos,
              title: response.data.title,
              filter: {},
            },
          },
        });
      }
      if (typeof response !== 'undefined' && response.code === 1) {
        const { bellWether } = response.data;
        // 筛选合计
        let marketvalue = 0;
        let todayprofit = 0;
        if (typeof payload.currentDataSourceBellWether !== 'undefined') {
          if (payload.currentDataSourceBellWether.length === 0) {
            for (let i = 0; i < bellWether.length; i++) {
              marketvalue += parseFloat(bellWether[i].marketvalue || 0);
              todayprofit += parseFloat(bellWether[i].todayprofit || 0);
            }
          } else {
            const code = _pull(
              _uniq(_map(payload.currentDataSourceBellWether, 'code')),
              null,
              undefined
            );
            for (let i = 0; i < bellWether.length; i++) {
              if (code.indexOf(bellWether[i].code) !== -1) {
                marketvalue += parseFloat(bellWether[i].marketvalue || 0);
                todayprofit += parseFloat(bellWether[i].todayprofit || 0);
              }
            }
          }
          bellWether.unshift({
            code: '合计:',
            marketvalue,
            todayprofit: todayprofit.toFixed(2),
          });
          yield put({
            type: 'newTwoPositionBellWether',
            payload: { list: { code: 1, message: response.message, list: bellWether, filter: {} } },
          });
        }
        if (typeof payload.currentDataSourceETF !== 'undefined') {
          const { etf } = response.data;
          // 筛选合计
          marketvalue = 0;
          todayprofit = 0;
          if (payload.currentDataSourceETF.length === 0) {
            for (let i = 0; i < etf.length; i++) {
              marketvalue += parseFloat(etf[i].marketvalue || 0);
              todayprofit += parseFloat(etf[i].todayprofit || 0);
            }
          } else {
            const code = _pull(_uniq(_map(payload.currentDataSourceETF, 'code')), null, undefined);
            for (let i = 0; i < etf.length; i++) {
              if (code.indexOf(etf[i].code) !== -1) {
                marketvalue += parseFloat(etf[i].marketvalue || 0);
                todayprofit += parseFloat(etf[i].todayprofit || 0);
              }
            }
          }
          etf.unshift({
            code: '合计:',
            marketvalue,
            todayprofit: todayprofit.toFixed(2),
          });
          yield put({
            type: 'newTwoPositionETF',
            payload: {
              list: {
                code: 1,
                message: response.message,
                list: etf,
                etf: { list: etf },
                filter: {},
              },
            },
          });
        }

        if (callback) callback(response);
      } else {
        notification.error({
          message: typeof response.message !== 'undefined' ? response.message : '请求错误',
        });
      }
    },

    *GetUBSNewTwoPositionModal({ payload, callback }, { call, put }) {
      const response = yield call(getLastTrade, payload);
      if (typeof response === 'undefined') {
        return;
      }

      if (typeof response !== 'undefined' && response.code === 1) {
        yield put({
          type: 'saveNewTwoPositionModal',
          payload: {
            list: { code: 1, message: response.message, list: response.data },
            filter: {},
          },
        });
        if (callback) callback(response);
      } else {
        notification.error({
          message: typeof response.message !== 'undefined' ? response.message : '请求错误',
        });
      }
    },

    *UpdateFundTwoInfo({ payload, callback }, { call, put }) {
      const response = yield call(updateFundTwoInfo, payload);
      if (typeof response === 'undefined') {
        return;
      }
      if (typeof response !== 'undefined' && response.code === 1) {
        message.success(response.message);
        if (callback) callback();
      } else {
        notification.error({
          message: typeof response.message !== 'undefined' ? response.message : '请求错误',
        });
      }
    },

    *UpdateLastTrade({ payload, callback }, { call, put }) {
      const response = yield call(updateLastTrade, payload);
      if (typeof response === 'undefined') {
        return;
      }
      if (typeof response !== 'undefined' && response.code === 1) {
        message.success(response.message);
        if (callback) callback();
      } else {
        notification.error({
          message: typeof response.message !== 'undefined' ? response.message : '请求错误',
        });
      }
    },

    *Insert({ payload, callback }, { call, put }) {
      message.loading('生成中..', 30);
      const response = yield call(insert, payload);
      if (typeof response === 'undefined') {
        return;
      }
      if (typeof response !== 'undefined' && response.code === 1) {
        message.destroy();
        message.success(response.message);
        if (callback) callback();
      } else {
        notification.error({
          message: typeof response.message !== 'undefined' ? response.message : '请求错误',
        });
      }
    },

    *InsertValueChart({ payload, callback }, { call, put }) {
      message.loading('生成中..', 30);
      const response = yield call(insertValueChart, payload);
      if (typeof response === 'undefined') {
        return;
      }
      if (typeof response !== 'undefined' && response.code === 1) {
        message.destroy();
        message.success(response.message);
        if (callback) callback();
      } else {
        notification.error({
          message: typeof response.message !== 'undefined' ? response.message : '请求错误',
        });
      }
    },

    // option
    *GetFssFundOptionsList({ payload, callback }, { call, put }) {
      const response = yield call(getFssFundOptionsList, { groupId: payload.groupId });
      if (typeof response === 'undefined') {
        return;
      }
      if (typeof response !== 'undefined' && response.code === 1) {
        let premium = 0;
        let sumprice = 0;
        let allpremium = 0;
        let remainpremium = 0;
        let allincome = 0;
        let thisincome = 0;
        if (payload.currentDataSource.length === 0) {
          for (let i = 0; i < response.data.length; i++) {
            if (response.data[i].name !== '期权合计') {
              premium += parseFloat(response.data[i].unkown2 || 0);
              sumprice += parseFloat(response.data[i].sumprice || 0);
              allpremium += parseFloat(response.data[i].allpremium || 0);
              remainpremium += parseFloat(response.data[i].remainpremium || 0);
              allincome += parseFloat(response.data[i].allincome || 0);
              thisincome += parseFloat(response.data[i].thisincome || 0);
            }
          }
        } else {
          for (let i = 0; i < payload.currentDataSource.length; i++) {
            if (payload.currentDataSource[i].name !== '期权合计') {
              premium += parseFloat(payload.currentDataSource[i].unkown2 || 0);
              sumprice += parseFloat(payload.currentDataSource[i].sumprice || 0);
              allpremium += parseFloat(payload.currentDataSource[i].allpremium || 0);
              remainpremium += parseFloat(payload.currentDataSource[i].remainpremium || 0);
              allincome += parseFloat(payload.currentDataSource[i].allincome || 0);
              thisincome += parseFloat(payload.currentDataSource[i].thisincome || 0);
            }
          }
        }

        response.data.unshift({
          name: '期权合计',
          unkown2: FormatnumberZero(premium, 2),
          sumprice: FormatnumberZero(sumprice, 2),
          allpremium: FormatnumberZero(allpremium, 2),
          remainpremium: FormatnumberZero(remainpremium, 2),
          allincome: FormatnumberZero(allincome, 2),
          thisincome: FormatnumberZero(thisincome, 2),
        });
        yield put({
          type: 'saveOptions',
          payload: {
            list: { code: 1, message: response.message, list: response.data },
            filter: {},
          } /* , pagination: {current: 1, pageSize: 30, total: response.data.length-1}, */,
        });
        if (callback) callback(response);
      } else {
        notification.error({
          message: typeof response.message !== 'undefined' ? response.message : '请求错误',
        });
      }
    },

    // charts
    *GetGroupFu({ payload, callback }, { call, put }) {
      const response = yield call(getGroupFu, payload);
      if (typeof response === 'undefined') {
        return;
      }
      const list = [];
      for (let i = 0; i < response.data.length; i += 1) {
        const newData = { list: [], name: '' };
        newData.name = response.data[i].name;
        for (let j = 0; j < response.data[i].list.length; j += 1) {
          newData.list.push({
            x: response.data[i].list[j].date,
            y1: response.data[i].list[j].fundpercent,
            y2: response.data[i].list[j].spypercent,
            y3: parseFloat(
              (response.data[i].list[j].fundpercent - response.data[i].list[j].spypercent).toFixed(
                6
              )
            ),
            y4: response.data[i].list[j].growth,
          });
        }
        list[i] = newData;
      }

      yield put({
        type: 'saveCharts',
        payload: list,
      });
      if (callback) callback();
    },

    // waitbuy fu
    *GetWaitBuy({ payload, callback }, { call, put }) {
      const response = yield call(getWaitBuy, payload);
      if (typeof response === 'undefined') {
        if (callback) callback(response);
        return;
      }
      if (typeof response !== 'undefined' && response.code === 1) {
        yield put({
          type: 'saveWaitBuy',
          payload: {
            list: response.list.waitBuyList,
            inx: response.list.inx,
            code: response.code,
            message: response.message,
          },
        });
        if (callback) callback(response);
      } else {
        notification.error({
          message: typeof response.message !== 'undefined' ? response.message : '请求错误',
        });
      }
    },
    *AddWaitBuy({ payload, callback }, { call, put }) {
      const response = yield call(addWaitBuy, payload);
      if (typeof response === 'undefined') {
        return;
      }
      if (typeof response !== 'undefined' && response.code === 1) {
        message.success(response.message);
        if (callback) callback();
      } else {
        notification.error({
          message: typeof response.message !== 'undefined' ? response.message : '请求错误',
        });
      }
    },
    *DeleteWaitBuy({ payload, callback }, { call, put }) {
      const response = yield call(deleteWaitBuy, payload);
      if (typeof response === 'undefined') {
        return;
      }
      if (typeof response !== 'undefined' && response.code === 1) {
        message.success(response.message);
        if (callback) callback();
      } else {
        notification.error({
          message: typeof response.message !== 'undefined' ? response.message : '请求错误',
        });
      }
    },
    *DeleteAllWaitBuy({ payload, callback }, { call, put }) {
      const response = yield call(deleteAllWaitBuy, payload);
      if (typeof response === 'undefined') {
        return;
      }
      if (typeof response !== 'undefined' && response.code === 1) {
        message.success(response.message);
        if (callback) callback();
      } else {
        notification.error({
          message: typeof response.message !== 'undefined' ? response.message : '请求错误',
        });
      }
    },

    // waitsale fu
    *GetWaitSale({ payload, callback }, { call, put }) {
      const response = yield call(getWaitBuy, payload);
      if (typeof response === 'undefined') {
        if (callback) callback(response);
        return;
      }
      yield put({
        type: 'saveWaitSale',
        payload: {
          list: response.list.waitBuyList,
          inx: response.list.inx,
          code: response.code,
          message: response.message,
        },
      });
      if (callback) callback(response);
    },

    // FuGroupHeadETF
    *GetFuGroupHeadETF({ payload, callback }, { call, put }) {
      const response = yield call(getFuGroupHeadETF, payload);
      if (typeof response === 'undefined') {
        if (callback) callback(response);
        return;
      }
      yield put({
        type: 'saveFuGroupHeadETF',
        payload: { list: response.data, code: response.code, message: response.message },
      });
      if (callback) callback(response);
    },
    *AddFuGroupHeadETF({ payload, callback }, { call, put }) {
      const response = yield call(addFuGroupHeadETF, payload);
      if (typeof response === 'undefined') {
        return;
      }
      if (typeof response !== 'undefined' && response.code === 1) {
        message.success(response.message);
        if (callback) callback();
      } else {
        notification.error({
          message: typeof response.message !== 'undefined' ? response.message : '请求错误',
        });
      }
    },
    *DeleteFuGroupHeadETF({ payload, callback }, { call, put }) {
      const response = yield call(deleteFuGroupHeadETF, payload);
      if (typeof response === 'undefined') {
        return;
      }
      if (typeof response !== 'undefined' && response.code === 1) {
        message.success(response.message);
        if (callback) callback();
      } else {
        notification.error({
          message: typeof response.message !== 'undefined' ? response.message : '请求错误',
        });
      }
    },
    *DeleteFuGroupHeadETFAll({ payload, callback }, { call, put }) {
      const response = yield call(deleteFuGroupHeadETFAll, payload);
      if (typeof response === 'undefined') {
        return;
      }
      if (typeof response !== 'undefined' && response.code === 1) {
        message.success(response.message);
        if (callback) callback();
      } else {
        notification.error({
          message: typeof response.message !== 'undefined' ? response.message : '请求错误',
        });
      }
    },

    // 二号期权
    *GetLastDayOptionList({ payload, callback }, { call, put }) {
      const response = yield call(getLastDayOptionList, payload);
      if (typeof response === 'undefined') {
        return;
      }
      if (typeof response !== 'undefined' && response.code === 1) {
        if (callback) callback(response);
      } else {
        notification.error({
          message: typeof response.message !== 'undefined' ? response.message : '请求错误',
        });
      }
    },
    *GetTodayTradeList({ payload, callback }, { call, put }) {
      const response = yield call(getTodayTradeList, payload);
      if (typeof response === 'undefined') {
        return;
      }
      if (typeof response !== 'undefined' && response.code === 1) {
        if (callback) callback(response);
      } else {
        notification.error({
          message: typeof response.message !== 'undefined' ? response.message : '请求错误',
        });
      }
    },
    *SyncLastOptionTradeInfo({ payload, callback }, { call, put }) {
      const response = yield call(syncLastOptionTradeInfo, payload);
      if (typeof response === 'undefined') {
        return;
      }
      if (typeof response !== 'undefined' && response.code === 1) {
        message.success(response.message);
        if (callback) callback(response);
      } else {
        notification.error({
          message: typeof response.message !== 'undefined' ? response.message : '请求错误',
        });
      }
    },
    *UpdateOptionFlag({ payload, callback }, { call, put }) {
      const response = yield call(updateOptionFlag, payload);
      if (typeof response === 'undefined') {
        return;
      }
      if (typeof response !== 'undefined' && response.code === 1) {
        message.success(response.message);
        if (callback) callback(response);
      } else {
        notification.error({
          message: typeof response.message !== 'undefined' ? response.message : '请求错误',
        });
      }
    },
    *MakeOptionTodayInfo({ payload, callback }, { call, put }) {
      const response = yield call(makeOptionTodayInfo, payload);
      if (typeof response === 'undefined') {
        return;
      }
      if (typeof response !== 'undefined' && response.code === 1) {
        message.success(response.message);
        if (callback) callback(response);
      } else {
        notification.error({
          message: typeof response.message !== 'undefined' ? response.message : '请求错误',
        });
      }
    },
  },

  reducers: {
    saveStocks(state, action) {
      return {
        ...state,
        stocks: action.payload.list,
        stocksFilters: action.payload.filter,
      };
    },

    saveNewTwoStocks(state, action) {
      return {
        ...state,
        newTwoStocks: action.payload.list,
        newTwoStocksFilters: action.payload.filter,
      };
    },

    newTwoPositionInfos(state, action) {
      return {
        ...state,
        newTwoPosition: {
          ...state.newTwoPosition,
          infos: action.payload.list,
        },
        newTwoPositionFilters: action.payload.filter,
      };
    },
    newTwoPositionBellWether(state, action) {
      return {
        ...state,
        newTwoPosition: {
          ...state.newTwoPosition,
          bellWether: action.payload.list,
        },
        newTwoPositionFilters: action.payload.filter,
      };
    },
    newTwoPositionETF(state, action) {
      return {
        ...state,
        newTwoPosition: {
          ...state.newTwoPosition,
          etf: action.payload.list,
        },
        newTwoPositionFilters: action.payload.filter,
      };
    },
    saveNewTwoPositionModal(state, action) {
      return {
        ...state,
        newTwoPositionModal: action.payload.list,
      };
    },

    saveOptions(state, action) {
      return {
        ...state,
        options: action.payload.list,
        optionsFilters: action.payload.filter,
      };
    },

    saveCharts(state, action) {
      return {
        ...state,
        charts: action.payload,
      };
    },

    saveWaitBuy(state, action) {
      return {
        ...state,
        waitbuy: action.payload,
      };
    },

    saveWaitSale(state, action) {
      return {
        ...state,
        waitsale: action.payload,
      };
    },

    saveFuGroupHeadETF(state, action) {
      return {
        ...state,
        fuGroupHeadETF: action.payload,
      };
    },
  },
};
